package com.anXin.runerrands.service.impl;

import cn.hutool.json.JSONUtil;
import com.anXin.apis.user.IUserClient;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.service.dtos.RunerrandsOrdersDTO;
import com.anXin.model.service.pojos.RunerrandsOrders;
import com.anXin.model.service.pojos.RunerrandsResort;
import com.anXin.model.user.dtos.UserInfoDto;
import com.anXin.runerrands.mapper.RunerrandsOrdersMapper;
import com.anXin.runerrands.service.RunerrandsOrdersService;
import com.anXin.runerrands.service.RunerrandsResortService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Slf4j
@Service
public class RunerrandsOrdersServiceImpl extends ServiceImpl<RunerrandsOrdersMapper, RunerrandsOrders> implements RunerrandsOrdersService {

    @Autowired
    @Lazy
    private RunerrandsResortService runerrandsResortService;

    @Resource
    private IUserClient iUserClient;

    /**
     * 生成/接收订单
     * @param runerrandsOrdersDTO
     * @return
     */
    @Override
    public ResponseResult addOrders(RunerrandsOrdersDTO runerrandsOrdersDTO) {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        if (runerrandsOrdersDTO.getServerId().equals(user.getId())) {
            RunerrandsResort runerrandsResort = runerrandsResortService.getById(runerrandsOrdersDTO.getPostId());
            runerrandsResort.setState(3);
            runerrandsResortService.updateById(runerrandsResort);
            RunerrandsOrders runerrandsOrders = new RunerrandsOrders();
            runerrandsOrders.setClientId(runerrandsResort.getAuthorId());
            runerrandsOrders.setClientPhone(runerrandsResort.getAuthorPhone());
            runerrandsOrders.setClientAddress(runerrandsResort.getAuthorAddress());
            runerrandsOrders.setServerId(runerrandsOrdersDTO.getServerId());
            runerrandsOrders.setServerPhone(user.getPhone());
            runerrandsOrders.setResultPhone(runerrandsResort.getResultPhone());
            runerrandsOrders.setResultAddress(runerrandsResort.getResultAddress());
            runerrandsOrders.setPrice(runerrandsResort.getPrice());
            runerrandsOrders.setType(runerrandsResort.getType());
            runerrandsOrders.setState(0);
            runerrandsOrders.setCreatedTime(new Date());
            runerrandsOrders.setPostId(runerrandsOrdersDTO.getPostId());
            runerrandsOrders.setContent(runerrandsResort.getContent());
            this.save(runerrandsOrders);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(), "订单已接单成功!");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(), "无权限操作");
        }
    }

    /**
     * 根据订单id查询订单
     * @param orderId
     * @return
     */
    @Override
    public ResponseResult getOrders(Long orderId) {
        RunerrandsOrders runerrandsOrders = this.getById(orderId);
        return ResponseResult.okResult(runerrandsOrders);
    }

    /**
     * 客户查看自己所有的订单
     * @return
     */
    @Override
    public ResponseResult getClientOrders() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<RunerrandsOrders> runerrandsOrders = query().eq("client_id",user.getId()).list();
        return ResponseResult.okResult(runerrandsOrders);
    }

    /**
     * 服务人员查看自己的接单
     * @return
     */
    @Override
    public ResponseResult getServerOrders() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<RunerrandsOrders> runerrandsOrders = query().eq("server_id",user.getId()).list();
        return ResponseResult.okResult(runerrandsOrders);
    }

    /**
     * 取消订单
     * @param postId
     * @return
     */
    @Override
    public ResponseResult cancelOrders(Long postId) {
        RunerrandsResort runerrandsResort = runerrandsResortService.getById(postId);
        runerrandsResort.setState(2);
        runerrandsResortService.updateById(runerrandsResort);
        RunerrandsOrders runerrandsOrders = query().eq("post_id",postId).one();
        runerrandsOrders.setState(2);
        this.updateById(runerrandsOrders);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"取消订单成功!");
    }
}
